import {
  add, edits, getquery, deletes, getid
} from '@/api/role'
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
import router, {
  resetRouter
} from '@/router'
import store from '@/store'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  introduction: '',
  roles: [],
  menus: []
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_MENU: (state, menus) => {
    state.menus = menus
  }
}
// 修改
const actions = {
  edits({
    commit
  }, userInfo) {
    const {
      role_id, role_name, treearray
    } = userInfo
    return new Promise((resolve, reject) => {
      edits({
        role_id, role_name, treearray
      }).then(response => {
        const {
          data
        } = response
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取单条信息
  getid({
    commit
  }, userInfo) {
    const {
      role_id
    } = userInfo
    return new Promise((resolve, reject) => {
      getid({
        role_id
      }).then(response => {
        const {
          data
        } = response
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取列表
  getquery({
    commit
  }, userInfo) {
    const {
      page,
      limit,
      role_name
    } = userInfo
    return new Promise((resolve, reject) => {
      getquery({
        page,
        limit,
        role_name
      }).then(response => {
        const {
          data
        } = response
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 新增
  add({
    commit
  }, userInfo) {
    const {
      role_name,
      treearray
    } = userInfo
    return new Promise((resolve, reject) => {
      add({
        role_name,
        treearray
      }).then(response => {
        const {
          data
        } = response
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 删除
  deletes({
    commit
  }, userInfo) {
    const {
      role_id
    } = userInfo
    return new Promise((resolve, reject) => {
      deletes({
        role_id
      }).then(response => {
        const {
          data
        } = response
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // dynamically modify permissions
  async changeRoles({
    commit,
    dispatch
  }, role) {
    const token = role + '-token'

    commit('SET_TOKEN', token)
    setToken(token)

    const {
      roles
    } = await dispatch('getInfo')

    resetRouter()

    // generate accessible routes map based on roles
    const accessRoutes = await dispatch('permission/generateRoutes', roles, {
      root: true
    })
    // dynamically add accessible routes
    router.addRoutes(accessRoutes)

    // reset visited views and cached views
    dispatch('tagsView/delAllViews', null, {
      root: true
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
